<template>
  <div ref="container" class="detail-view">
    <div v-if="loading" class="produc-view">
      <div class="nav-list" :style="{ height: productStyle.height }">
        <div
          v-for="(item, i) in goodInfo.Images"
          :key="i + 't'"
          class="nav-i"
          :style="imgStyle"
          :class="min === item.LargePic ? 'active' : ''"
          @click="min = item.LargePic"
        >
          <icons
            v-if="item.LargePic.includes('.mp4')"
            class="icon"
            name="zant"
          />
          <video
            v-if="item.LargePic.includes('.mp4')"
            :src="item.LargePic"
          ></video>
          <img v-else :src="item.LargePic" alt="" />
        </div>
      </div>
      <div class="zoom-img" :style="productStyle">
        <video
          v-if="min.includes('.mp4')"
          muted
          :src="min"
          loop
          autoplay
        ></video>
        <zoom-on-hover
          v-else-if="min"
          :img-normal="min"
          :imgs="goodInfo.Images"
        />
      </div>
      <div class="right-view">
        <div class="r-view-box" :style="boxStyle">
          <div class="title">{{ goodInfo.GoodsName }}</div>
          <div class="desc">{{ goodInfo.Introduce }}</div>
          <div class="price">{{ goodInfo.Price }}元</div>
          <div class="color">
            <img :src="goodInfo.Color[0].PicPath" alt="" />
          </div>
          <div class="c-title">尺寸：</div>
          <div class="c-view">
            <div v-for="(item, i) in goodInfo.Size" :key="i" class="tag">
              {{ item }}
            </div>
          </div>
          <div class="btn">选购</div>
          <div class="text">{{ goodInfo.Color[0].ColorName }}</div>
          <div class="text-d">
            {{ goodInfo.Color[0].Introduce }}
          </div>
          <Collapse :collapseData="collapseData" v-model:activeKey="key" />
        </div>
      </div>
    </div>
    <div v-if="loading" class="swiper-view">
      <div class="title-view">
        <div class="title">畅销产品</div>
        <div class="right">
          <icons
            @click="prevSlide"
            class="icon"
            :class="isBeginning ? 'y' : ''"
            name="jiantou2"
          />
          <icons
            @click="nextSlide"
            class="icon"
            :class="isEnd ? 'y' : ''"
            name="jiantou"
          />
        </div>
        <!-- 如果需要导航按钮 -->
      </div>
      <swiper
        :slidesPerView="4"
        :spaceBetween="15"
        :freeMode="true"
        :pagination="{
          clickable: true,
        }"
        :navigation="true"
        :modules="modules"
        class="mySwiper"
        @swiper="setControlledSwiper"
      >
        <swiper-slide
          v-for="pro in productList"
          :key="pro.Id"
          @click="toPage('/detail/' + pro.Id)"
        >
          <img class="swiper-img" :src="pro.IndexPic" alt="" />
          <div class="title">{{ pro.GoodsName }}</div>
          <div class="desc-box">
            <div class="desc">{{ pro.Introduce }}</div>
            <div class="price">{{ pro.Price }}元</div>
          </div>
        </swiper-slide>
      </swiper>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, onMounted, watchEffect } from "vue";
import { useRouter, useRoute } from "vue-router";
import { GetGoodsDetail, GetRandomGoods } from "@/server";
import Collapse from "@/components/Collapse/index.vue";
import { Swiper, SwiperSlide } from "swiper/swiper-vue";
import { FreeMode, Navigation } from "swiper/modules";
import ZoomOnHover from "@/components/ImagMax/ZoomOnHover.vue";
// Import Swiper styles
import "swiper/swiper.css";
import { useAppStore } from "@/store/modules/user";
const store = useAppStore();
const route = useRoute();
const value = computed(() => route.params.id);
const router = useRouter();
const loading = ref(false);
const swiperRef = ref(null) as any;
const modules = [FreeMode, Navigation];
const min = ref("");
const collapseData = ref([]);
const goodInfo = ref({}) as any;
const GetGoodsDetailFun = async () => {
  const res: any = await GetGoodsDetail({ Id: value.value });
  if (res.Tag === 1) {
    const data = res.Data;
    console.log(" data.Size", data.Size);

    if (data.Size) {
      data.Size = data.Size.split(",");
    }
    min.value = data.Images[0].LargePic;
    collapseData.value = data.Notice.map((e: any, i: number) => {
      return {
        key: i + "aa",
        header: e.Title,
        text: e.IntroduceList,
      };
    });
    goodInfo.value = data;
    loading.value = true;
  } else {
    loading.value = true;
    store.error(res.Message); // 错误提示
  }
};
const productList = ref([]) as any;
const GetGoodsByConditionFun = async () => {
  const res: any = await GetRandomGoods({
    RowCount: 10, //  随机的个数，前端来定
  });
  if (res.Tag === 1) {
    productList.value = res.Data;
  } else {
    store.error(res.Message); // 错误提示
  }
};
const setControlledSwiper = (swiper: any) => {
  swiperRef.value = swiper;
};
const isBeginning = ref(true);
const isEnd = ref(false);
// 调用上一页事件
const prevSlide = () => {
  if (swiperRef.value) {
    swiperRef.value.slidePrev();
    isBeginning.value = swiperRef.value.isBeginning;
    isEnd.value = swiperRef.value.isEnd;
  }
};

// 调用下一页事件
const nextSlide = () => {
  if (swiperRef.value) {
    swiperRef.value.slideNext();
    isBeginning.value = swiperRef.value.isBeginning;
    isEnd.value = swiperRef.value.isEnd;
  }
};
GetGoodsByConditionFun();
GetGoodsDetailFun();
const container = ref<HTMLElement | null>(null);
const productStyle = ref({ minWidth: "1030px", height: "1364px" });
const imgStyle = ref({ width: "190px", height: "250px" });
const boxStyle = ref({ width: "420px" });
const activeKey = ref(["1"]);
watchEffect(() => {
  GetGoodsDetailFun();
});
const key = ref("1");
/**
 * @description: 页面跳转实例
 * @param {*} url
 * @return {*}
 * @author: cool panda
 */
const toPage = (url: string) => {
  window.open(url, "_blank");
};
const handleResize: ResizeObserverCallback = (entries) => {
  if (entries[0].target) {
    const containerWidth = entries[0].contentRect.width;
    const w = containerWidth * 0.53;
    const r = 1030 / 1364;
    const w1 = containerWidth * 0.09;
    const r1 = 190 / 250;
    productStyle.value = { minWidth: w + "px", height: w / r + "px" };
    imgStyle.value = { width: w1 + "px", height: w1 / r1 + "px" };
    boxStyle.value = { width: containerWidth * 0.21 + "px" };
  }
};
onMounted(() => {
  const resizeObserver = new ResizeObserver(handleResize);
  if (container.value) {
    resizeObserver.observe(container.value);
  }

  return () => {
    if (container.value) {
      resizeObserver.unobserve(container.value);
    }
    resizeObserver.disconnect();
  };
});
</script>
<style lang="less" scoped>
.detail-view {
  margin-top: 30px;
  width: 100%;
  .produc-view {
    width: 100%;
    display: flex;
    .nav-list {
      padding: 0 20px;
      overflow-y: auto;
      .nav-i {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #f8f8f8;
        .icon {
          position: absolute;
          width: 60px;
          height: 60px;
          // background: #ffffff;
        }
        &.active {
          border: 2px solid rgb(54, 197, 29);
        }
        margin-bottom: 10px;
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        video {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
    .zoom-img {
      width: 53%;
      height: 1364px;
    }
    .right-view {
      flex: 1;
      display: flex;
      justify-content: center;
      margin-top: 33px;
      .r-view-box {
        text-align: left;
        .title {
          font-size: 20px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #000000;
          line-height: 33px;
        }
        .desc {
          margin-top: 6px;
          font-size: 13px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #555555;
          line-height: 20px;
        }
        .price {
          margin-top: 14px;
          font-size: 13px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #000000;
          line-height: 20px;
        }
        .color {
          margin-top: 50px;
          margin-bottom: 26px;
          img {
            width: 46px;
            height: 46px;
            border-radius: 50%;
            border: 2px solid #000000;
          }
        }
        .c-title {
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #000000;
          line-height: 21px;
          margin-bottom: 10px;
        }
        .c-view {
          display: flex;
          flex-wrap: wrap;
          .tag {
            font-size: 13px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #000000;
            line-height: 20px;
            margin-right: 50px;
          }
        }
        .btn {
          &:hover {
            color: #000;
            background: #ffffff;
            border: 1px solid #979797;
          }
          cursor: pointer;
          transition: 0.3s all;
          display: flex;
          justify-content: center;
          align-items: center;
          margin: 30px 0 12px;
          width: 100%;
          height: 50px;
          background: #000000;
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 20px;
        }
        .text {
          display: inline;
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #000000;
          line-height: 21px;
          padding-bottom: 3px;
          border-bottom: 1px solid #000000;
        }
        .text-d {
          margin-top: 15px;
          font-size: 13px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #000000;
          line-height: 20px;
          margin-bottom: 50px;
        }
      }
    }
  }
  .swiper-view {
    margin-top: 100px;
    padding: 0 60px;
    .title-view {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .title {
        text-align: left;
        font-size: 22px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #000000;
        margin-bottom: 18px;
      }
      .icon {
        &.y {
          opacity: 0.3;
        }
        cursor: pointer;
        width: 28px;
        height: 16px;
        margin-left: 20px;
      }
    }
    .mySwiper {
      text-align: left;
      .swiper-img {
        width: 100%;
        object-fit: cover;
      }
      .title {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: bold;
        color: #000000;
        line-height: 25px;
        width: 100%;
        white-space: nowrap; /* 防止文字换行 */
        overflow: hidden; /* 隐藏溢出部分 */
        text-overflow: ellipsis; /* 显示省略号 */
        margin-top: 8px;
      }
      .desc-box {
        display: flex;
        align-items: center;
        margin-top: 4px;
        .desc {
          flex: 1;
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
          color: #b1b1b1;
          white-space: nowrap; /* 防止文字换行 */
          overflow: hidden; /* 隐藏溢出部分 */
          text-overflow: ellipsis; /* 显示省略号 */
        }
        .price {
          font-size: 12px;
          font-family: PingFangSC-Semibold, PingFang SC;
          color: #000000;
          line-height: 20px;
        }
      }
      /*先去掉默认样式*/
    }
  }
  video {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>
<style>
.swiper-button-prev:after {
  display: none;
}
.swiper-button-next:after {
  display: none;
}
/*再自定义样式*/
.swiper-button-prev {
  width: 30px;
  height: 30px;
}
.swiper-button-next {
  width: 30px;
  height: 30px;
}
</style>
